package com.baofu.paycore.manager.model.preAuth;

import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.manager.model.PayBaseBO;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.google.common.base.Objects;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 预授权模型
 *
 * User: xin deng  Date: 16/8/30 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Setter
@Getter
@ToString(callSuper = true)
public class PreAuthBO extends PayBaseBO {

    /**
     * 付款方客户账户号
     */
    private String payerNo;

    /**
     * 原支付单号
     */
    private String origPayNo;

    /**
     * 已处理金额
     */
    private Long dealAmt;

    /**
     * 预授权可处理金额
     */
    private Long authAmt;


    /**
     * 扩展字段
     */
    private String depositExtra;

    /**
     * 资金单
     */
    private List<SubPreAuthBO> subPreAuthBO;

    /**
     * 根据资金明细ID从资金明细列表中获取对应的资金明细
     *
     * @param list         资金明细列表
     * @param fundDetailNo 资金明细NO
     * @return             资金明细
     */
    public SubPreAuthBO getSubPreAuth(List<SubPreAuthBO> list, String fundDetailNo) {

        if (CollectionUtils.isEmpty(list)) {
            log.error("预授权子单不存在:{}",fundDetailNo);
            throw new PayCoreBizException(PayCoreErrorCode.VALUE_NOT_SUPPORT);
        }
        for (SubPreAuthBO subPreAuthBO : list){
            if (Objects.equal(fundDetailNo, subPreAuthBO.getFundDetailNo())){
                return subPreAuthBO;
            }
        }
        log.error("资金单不存在:{}", fundDetailNo);
        throw new PayCoreBizException(PayCoreErrorCode.VALUE_NOT_SUPPORT);
    }

    /**
     * 模型检查
     */
    @Override
    public void consistencyCheck() {

    }

    /**
     * 状态检查
     */
    @Override
    public void checkStatus() {

    }
}
